import * as THREE from 'three';

export class SceneInspector {
    constructor(sceneManager) {
        this.sceneManager = sceneManager;
        this.raycaster = new THREE.Raycaster();
        this.mouse = new THREE.Vector2();
        this.domElement = sceneManager.renderer?.domElement || document.body;
        this.lastHitInfo = null;
        this.overlay = null;
        this.enabled = false;
        this.onClick = this.onClick.bind(this);
    }

    enable() {
        if (this.enabled) return;
        this.enabled = true;
        this.createOverlay();
        this.domElement.addEventListener('click', this.onClick);
    }

    disable() {
        if (!this.enabled) return;
        this.enabled = false;
        this.domElement.removeEventListener('click', this.onClick);
        if (this.overlay && this.overlay.parentNode) {
            this.overlay.parentNode.removeChild(this.overlay);
        }
        this.overlay = null;
    }

    onClick(event) {
        if (!this.sceneManager || !this.sceneManager.camera || !this.sceneManager.sceneGroup) return;
        const rect = this.domElement.getBoundingClientRect();
        this.mouse.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
        this.mouse.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
        this.raycaster.setFromCamera(this.mouse, this.sceneManager.camera);
        const intersects = this.raycaster.intersectObjects(this.sceneManager.sceneGroup.children, true);
        if (!intersects || intersects.length === 0) return;
        const hit = intersects[0];
        const world = hit.point.clone();
        const local = hit.object.worldToLocal(hit.point.clone());
        const path = this.buildPath(hit.object);
        const info = {
            world,
            local,
            objectName: hit.object.name || '',
            path
        };
        this.lastHitInfo = info;
        this.updateOverlay(info);
        this.moveFirstVisibleCharacter(world);
        console.log('坐标采样', {
            world: { x: world.x, y: world.y, z: world.z },
            local: { x: local.x, y: local.y, z: local.z },
            object: info.objectName,
            path
        });
    }

    moveFirstVisibleCharacter(worldPoint) {
        const group = this.sceneManager.characterGroup;
        if (!group || !group.children || group.children.length === 0) return;
        const target = group.children.find(c => c.visible);
        if (!target) return;
        const pos = { x: worldPoint.x, y: worldPoint.y, z: worldPoint.z };
        if (this.sceneManager.characterLoader && typeof this.sceneManager.characterLoader.setCharacterPosition === 'function') {
            this.sceneManager.characterLoader.setCharacterPosition(target, pos);
        } else {
            target.position.set(pos.x, pos.y, pos.z);
        }
    }

    buildPath(object) {
        const parts = [];
        let cur = object;
        while (cur) {
            parts.push(cur.name || cur.type);
            cur = cur.parent;
        }
        return parts.reverse().join(' / ');
    }

    createOverlay() {
        const el = document.createElement('div');
        el.id = 'coordinate-inspector';
        el.style.position = 'fixed';
        el.style.right = '12px';
        el.style.top = '12px';
        el.style.background = 'rgba(0,0,0,0.6)';
        el.style.color = '#fff';
        el.style.fontSize = '12px';
        el.style.padding = '8px 10px';
        el.style.borderRadius = '6px';
        el.style.zIndex = '9999';
        el.style.pointerEvents = 'none';
        el.textContent = '坐标: -';
        document.body.appendChild(el);
        this.overlay = el;
    }

    updateOverlay(info) {
        if (!this.overlay || !info) return;
        const w = info.world;
        const o = info.objectName || '-';
        this.overlay.textContent = `世界: ${w.x.toFixed(3)}, ${w.y.toFixed(3)}, ${w.z.toFixed(3)} | 对象: ${o}`;
    }
}

